package com.lagou.sqlSession;

import com.lagou.pojo.Configuration;

import java.sql.Connection;
import java.sql.SQLException;

public class DefaultSqlSessionFactory implements SqlSessionFactory {

    private Configuration configuration;

    public DefaultSqlSessionFactory(Configuration configuration) {
        this.configuration = configuration;
    }


    @Override
    public SqlSession openSession() throws SQLException {
        return openSession(false);
    }

    @Override
    public SqlSession openSession(boolean autoCommit) throws SQLException {
        Connection connection = configuration.getDataSource().getConnection();
        // 设置是否自动提交事务
        connection.setAutoCommit(autoCommit);
        Executor executor = new simpleExecutor(connection);
        return new DefaultSqlSession(configuration, executor);
    }
}
